package com.lqq.util;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

public class ComBoxUtil
{
    public static JComboBox creatComBox(String[] item ){
        String[] items=item;
        //创建下拉
        final JComboBox comboBox=new JComboBox(items);
        comboBox.setEditable(true);
        //添加选中的监听器
        comboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                //只处理选中的
                if(e.getStateChange()==ItemEvent.SELECTED){
                    System.out.println(comboBox.getSelectedItem());
                }
            }
        });
        //设置comboBox默认选中
        comboBox.setSelectedIndex(0);
        return comboBox;
    }
}
